using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using THSERP.BaseProvider;

namespace THSERP
{
    public partial class guiProductInventory : THSERPScreen
    {
        public THSList<ICProductAttributesInfo> AttributeList = new THSList<ICProductAttributesInfo>();
        public int ICStockID;

        public guiProductInventory()
        {
            InitializeComponent();
        }

        public guiProductInventory(int productID, int stockID)
        {
            InitializeComponent();

            fld_lkeFK_ICProductID.Properties.DataSource = ((DataSet)THSApp.LookupTables[Modules.Product.ProductEntities.strICProductsObjectName]).Tables[0];
            fld_lkeFK_ICProductID.EditValue = productID;
            ICStockID = stockID;
            
            InvalidateProductInventory(productID);
        }

        public void InvalidateProductInventory(int productID)
        {
            ProductAttributeList lst = new ProductAttributeList();
            lst.InitTHSList(null, Modules.Product.ProductEntities.strICProductsObjectName, Modules.Product.ProductEntities.strICProductAttributesObjectName);
            lst.InvalidateTreeList(productID, ICStockID, false);
            
            THSList<THSTreeListObject> lstLastAttributes = new THSList<THSTreeListObject>();
            lst.GetLastNodes(lstLastAttributes);
            AttributeList.Clear();
            if (lstLastAttributes.Count > 0)
            {
                foreach (THSTreeListObject obj in lstLastAttributes)
                {
                    ICProductAttributesInfo objProductAttributesInfo = (ICProductAttributesInfo)obj;
                    objProductAttributesInfo.ICInventoryStockAvailableQuantity = objProductAttributesInfo.ICInventoryStockQuantity - objProductAttributesInfo.ICInventoryStockSaleOrderQuantity;
                    if (objProductAttributesInfo.ICInventoryStockAvailableQuantity > 0)
                        AttributeList.Add(objProductAttributesInfo);
                }
            }
            //Product has no attribute
            else
            {
                ICProductAttributesController objProductAttributesController = new ICProductAttributesController();
                ICProductAttributesInfo objProductAttributesInfo = new ICProductAttributesInfo();
                objProductAttributesInfo.FK_ICProductID = productID;
                objProductAttributesInfo.ICProductAttributeValue = String.Empty;
                objProductAttributesInfo.ICInventoryStockQuantity = objProductAttributesController.CalculateStockQuantity(ICStockID, objProductAttributesInfo);
                objProductAttributesInfo.ICInventoryStockSaleOrderQuantity = objProductAttributesController.CalculateStockSOQuantity(ICStockID, objProductAttributesInfo);
                objProductAttributesInfo.ICInventoryStockAvailableQuantity = objProductAttributesInfo.ICInventoryStockQuantity - objProductAttributesInfo.ICInventoryStockSaleOrderQuantity;
                if (objProductAttributesInfo.ICInventoryStockAvailableQuantity > 0)
                    AttributeList.Add(objProductAttributesInfo);
            }
            fld_dgcICProductAttributes.DataSource = AttributeList;
            fld_dgcICProductAttributes.RefreshDataSource();
        }

        private void fld_lkeFK_ICProductID_Validated(object sender, EventArgs e)
        {
            int productID = Convert.ToInt32(fld_lkeFK_ICProductID.EditValue);
            if (productID > 0)
                InvalidateProductInventory(productID);
        }

        private void fld_btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void fld_btnAdd_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}